package com.xs.bookspoststation.UserActivity2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.db.DBHelperUser;

public class UpdateUserPwdActivity extends AppCompatActivity {

    private Button bt1;
    private EditText ued1,ued2,ued3;
    private DBHelperUser dbh;
//    private SQLiteDatabase db;
//    private Cursor cu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user_pwd);
        //绑定控件
        ued1 = findViewById(R.id.et_user);//用户名
        ued2 = findViewById(R.id.et_pwd);//新密码
        ued3 = findViewById(R.id.et_pwd2);//确认密码
        bt1 = findViewById(R.id.btn_update);//修改按钮
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goup();
            }

            private void goup() {
                String user = ued1.getText().toString().trim();
                String pawd = ued2.getText().toString().trim();
                String repawd = ued3.getText().toString().trim();
                System.out.println(user+"用户名");
                dbh = new DBHelperUser(UpdateUserPwdActivity.this,"books_station.db",null,1);
                SQLiteDatabase db = dbh.getReadableDatabase();
                boolean updatemima = false;
                Cursor cu = db.query("book_user",new String[]{"username"},null,null,null,null,null,null);
                while (cu.moveToNext()){
                    if (!user.equals(cu.getString(cu.getColumnIndex("username")))){
                        Toast.makeText(UpdateUserPwdActivity.this, "您输入的账号有误！", Toast.LENGTH_SHORT).show();
                        break;
                    }else {
                        if (pawd.equals(repawd)){
                            if (pawd.length() > 5){
                                ContentValues values = new ContentValues();
                                values.put("userpwd",pawd);
                                String where = "username='"+user+"'";
                                db.update("book_user",values,where,null);
                                updatemima = true;
                                Toast.makeText(UpdateUserPwdActivity.this, "密码修改成功！", Toast.LENGTH_SHORT).show();
                                db.close();
                                ued1.setText("");
                                ued2.setText("");
                                ued3.setText("");
                                break;
                            }else {
                                Toast.makeText(UpdateUserPwdActivity.this, "请输入六位密码", Toast.LENGTH_SHORT).show();
                                break;
                            }
                        }else {
                            Toast.makeText(UpdateUserPwdActivity.this, "两次密码不匹配！", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });

    }
}
